# pip install openpyxl ezdxf
import ezdxf
import openpyxl

# 打开 Excel 文件
wb = openpyxl.load_workbook("test.xlsx")

# 获取特定列的数据
sheet = wb.active
column_data = [cell.value for cell in sheet["A"]]
print(column_data)
# 提取点坐标
for cell in sheet["A"]:
    dxfName = sheet[("B" + str(cell.row))].value
    print(dxfName)
    data = cell.value
    print(data)
    
    data_list = data.split(',')
    print(data_list)
    
    # 提取数据
    total_points = float(data_list[1])
    elevation_range = (float(data_list[-5]), float(data_list[-4]))
    coordinates = [(float(data_list[i+2]), float(data_list[i+1])) for i in range(2, len(data_list)-5, 3)]
    # coordinates = [(float(data_list[i+1]), float(data_list[i+2])) for i in range(2, len(data_list)-5, 3)]
    print(coordinates)
    
    # 创建CAD图纸
    doc = ezdxf.new('R2000')
    msp = doc.modelspace()
    
    ## 投影并连线
    polyline = msp.add_lwpolyline(coordinates)
    
    # 多段线闭合
    polyline.close()
    
    # 保存CAD文件
    doc.saveas(str(dxfName) + ".dxf")
